#!/bin/bash

list[1]=one
list[2]=two
list[3]=three
list[4]=four
list[5]=five
list[6]=six
list[7]=seven
list[8]=eight
list[9]=nine
list[10]=ten
list[11]=eleven
list[12]=twelve

begin=2
loop=12

for i in $(seq $begin $loop); do
  hive -e "
use novel;
set hive.exec.mode.local.auto = true;
set hive.exec.mode.local.auto.inputbytes.max = 5368709120;
set hive.exec.mode.local.auto.input.files.max = 20;
set hive.auto.convert.join=false;
drop table if exists ${list[i]}_tmp;
create table if not exists ${list[i]}_tmp
as
select substr_words(sentence, ${i}) words from sentences_of_Chinese;

set io.sort.mb = 10;
drop table if exists ${list[i]}_count;
create table if not exists ${list[i]}_count
as
select words, count(*) count from ${list[i]}_tmp group by words;
set io.sort.mb = 100;

drop table if exists ${list[i]}_confidence;
create table if not exists ${list[i]}_confidence
as
select w.words, w.count,
       w.word_first, w.count/w1.count word_first_conf, w.word_second, w.count/w2.count word_second_conf
from
    (
        select words, count,
               substr(words, 1, $((i-1))) word_first, substr(words, 2, $((i-1))) word_second
        from ${list[i]}_count
    ) w
        left join ${list[i-1]} w1 on w.word_first=w1.words
        left join ${list[i-1]} w2 on w.word_second=w2.words;

insert overwrite table ${list[i]}_confidence
select * from ${list[i]}_confidence where words is not null;

set io.sort.mb = 10;
insert into relationship partition (locate = $((i*2-4)))
select b.word_first, max(words)
from ${list[i]}_confidence b
         right join (
    select word_first, max(word_first_conf) max_conf
    from ${list[i]}_confidence
    group by word_first
) a on b.word_first = a.word_first and b.word_first_conf = a.max_conf
group by b.word_first;

insert into relationship partition (locate = $((i*2-3)))
select b.word_second, max(words)
from ${list[i]}_confidence b
         right join (
    select word_second, max(word_second_conf) max_conf
    from ${list[i]}_confidence
    group by word_second
) a on b.word_second = a.word_second and b.word_second_conf = a.max_conf
group by b.word_second;
set io.sort.mb = 100;

drop table if exists ${list[i]};
create table if not exists ${list[i]}
as
select words, count from ${list[i]}_confidence;

drop table if exists ${list[i]}_confidence;
drop table if exists ${list[i]}_count;
drop table if exists ${list[i]}_tmp;
set hive.auto.convert.join=true;
"

done

for i in $(seq $begin $loop); do
hive -e "
use novel;set hive.exec.mode.local.auto = true;
drop table if exists ${list[i]};
"
done

# hive -e "hql"  hql中含有--注释时，会报错：
# NoViableAltException(-1@[])
# FAILED: ParseException line cannot recognize input near
